using MicroMsg.Common.Event;
using MicroMsg.UI.Util;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
namespace MicroMsg.UI.UserContrl
{
	public class ListSearchItemContainer : UserControl
	{
		public delegate void ListItemClick(object sender, object object_0);
		private const string TAG = "ListSearchItemContainer";
		private object mObjectLocker = new object();
		public static DependencyProperty ItemObjectProperty = DependencyProperty.Register("ItemObject", typeof(SearchRetItem), typeof(ListSearchItemContainer), new PropertyMetadata(null, new PropertyChangedCallback(ListSearchItemContainer.OnItemObjectPropertyChanged)));
		private bool mBIsFirstLoaded = true;
		private EventWatcher mWatchxxx;
		private object mObjectWatcher;
		internal Grid gridContainer;
		private bool _contentLoaded;
		public event ListSearchItemContainer.ListItemClick ItemClick;
		public SearchRetItem ItemObject
		{
			get
			{
				return (SearchRetItem)base.GetValue(ListSearchItemContainer.ItemObjectProperty);
			}
			set
			{
				base.SetValue(ListSearchItemContainer.ItemObjectProperty, value);
			}
		}
		public ListSearchItemContainer()
		{
			this.InitializeComponent();
			this.getCtrlItem();
			base.add_Loaded(new RoutedEventHandler(this.Control_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Control_Unloaded));
		}
		private void removeCtrl()
		{
			try
			{
				lock (this.mObjectLocker)
				{
					if (this.gridContainer.get_Children().get_Count() == 1 && this.gridContainer.get_Children().get_Item(0) is ListSearchItem)
					{
						ListSearchItem item = this.gridContainer.get_Children().get_Item(0) as ListSearchItem;
						ListSearchItem.PushOneItem(item);
					}
					this.gridContainer.get_Children().Clear();
				}
			}
			catch
			{
			}
		}
		private void getCtrlItem()
		{
			lock (this.mObjectLocker)
			{
				if (this.gridContainer.get_Children().get_Count() != 1 || !(this.gridContainer.get_Children().get_Item(0) is ListSearchItem))
				{
					if (this.gridContainer.get_Children().get_Count() > 1)
					{
						this.gridContainer.get_Children().Clear();
					}
					this.gridContainer.get_Children().Add(ListSearchItem.GetOneItem());
				}
			}
		}
		private void setCtrlItem()
		{
			lock (this.mObjectLocker)
			{
				if (this.gridContainer.get_Children().get_Count() != 1)
				{
					this.getCtrlItem();
				}
				if (this.gridContainer.get_Children().get_Count() == 1 && this.gridContainer.get_Children().get_Item(0) is ListSearchItem)
				{
					ListSearchItem listSearchItem = this.gridContainer.get_Children().get_Item(0) as ListSearchItem;
					listSearchItem.ItemObject = this.ItemObject;
				}
			}
		}
		~ListSearchItemContainer()
		{
		}
		private static void OnItemObjectPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == ListSearchItemContainer.ItemObjectProperty)
			{
				(dependencyObject_0 as ListSearchItemContainer).ItemObjectPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void ItemObjectPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_OldValue() != dependencyPropertyChangedEventArgs_0.get_NewValue())
			{
				if (this.ItemObject != null)
				{
					this.setCtrlItem();
				}
				else
				{
					this.removeCtrl();
				}
			}
			if (dependencyPropertyChangedEventArgs_0.get_NewValue() == null)
			{
				this.removeCtrl();
			}
		}
		private void Control_Loaded(object sender, RoutedEventArgs e)
		{
			this.setCtrlItem();
		}
		private void Control_Unloaded(object sender, RoutedEventArgs e)
		{
			this.removeCtrl();
		}
		private void registerEvent()
		{
			if (this.mWatchxxx == null)
			{
				this.mWatchxxx = new EventWatcher(this, null, new EventHandlerDelegate(this.Onxxx));
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatchxxx);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchxxx != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatchxxx);
				this.mWatchxxx = null;
			}
		}
		private void Onxxx(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
		}
		private void btnLayoutRoot_Click(object sender, RoutedEventArgs e)
		{
			if (this.ItemClick != null)
			{
				this.ItemClick(this, this.ItemObject);
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/ListSearchHelper/ListSearchItemContainer.xaml", 2));
			this.gridContainer = (Grid)base.FindName("gridContainer");
		}
	}
}
